﻿namespace Huayou.Common.Database
{
    using System;
    using System.Data;
    using System.Data.OleDb;

    public class OleDBOperate : DBOperate
    {
        private bool bInTrans = false;
        private OleDbConnection conn;
        private OleDbTransaction trans;

        public OleDBOperate(string strConnection)
        {
            this.conn = new OleDbConnection(strConnection);
        }

        public override void BeginTran()
        {
            if (!this.bInTrans)
            {
                this.trans = this.conn.BeginTransaction();
                this.bInTrans = true;
            }
        }

        public override void Close()
        {
            if (this.conn.State.Equals(ConnectionState.Open))
            {
                this.conn.Close();
            }
        }

        public override void CommitTran()
        {
            if (this.bInTrans)
            {
                this.trans.Commit();
                this.bInTrans = false;
            }
        }

        public override void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (this.bInTrans && (this.trans != null))
                {
                    this.trans = null;
                }
                if (this.conn != null)
                {
                    if (this.conn.State == ConnectionState.Open)
                    {
                        this.conn.Close();
                    }
                    this.conn.Dispose();
                    this.conn = null;
                }
            }
        }

        public override IDataReader ExecPreQueryForDataReader(string strSql, IDataParameter[] parameters)
        {
            return this.GetPreCommand(strSql, parameters, "SQL").ExecuteReader();
        }

        public override DataSet ExecPreQueryForDataSet(string strSql, IDataParameter[] parameters, string strTableName)
        {
            DataSet dataSet = new DataSet();
            OleDbDataAdapter adapter = new OleDbDataAdapter(strSql, this.conn);
            OleDbCommand command = this.GetPreCommand(strSql, parameters, "SQL");
            adapter.SelectCommand = command;
            adapter.Fill(dataSet, strTableName);
            return dataSet;
        }

        public override DataTable ExecPreQueryForDataTable(string strSql, IDataParameter[] parameters)
        {
            DataTable dataTable = new DataTable("returnTable");
            OleDbDataAdapter adapter = new OleDbDataAdapter(strSql, this.conn);
            OleDbCommand command = this.GetPreCommand(strSql, parameters, "SQL");
            adapter.SelectCommand = command;
            adapter.Fill(dataTable);
            return dataTable;
        }

        public override int ExecPreUpdateSql(string strSql, IDataParameter[] parameters)
        {
            return this.GetPreCommand(strSql, parameters, "SQL").ExecuteNonQuery();
        }

        public override IDataReader ExecProcForDataReader(string strSql, IDataParameter[] parameters)
        {
            return this.GetPreCommand(strSql, parameters, "PROCEDURE").ExecuteReader();
        }

        public override DataSet ExecProcForDataSet(string strSql, IDataParameter[] parameters, string strTableName)
        {
            DataSet dataSet = new DataSet();
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            OleDbCommand command = this.GetPreCommand(strSql, parameters, "PROCEDURE");
            adapter.SelectCommand = command;
            adapter.Fill(dataSet, strTableName);
            return dataSet;
        }

        public override DataTable ExecProcForDataTable(string strSql, IDataParameter[] parameters)
        {
            DataTable dataTable = new DataTable();
            OleDbDataAdapter adapter = new OleDbDataAdapter(strSql, this.conn);
            OleDbCommand command = this.GetPreCommand(strSql, parameters, "PROCEDURE");
            adapter.SelectCommand = command;
            adapter.Fill(dataTable);
            return dataTable;
        }

        public override IDataReader ExecQueryForDataReader(string strSql)
        {
            OleDbCommand command = this.conn.CreateCommand();
            command.CommandText = strSql;
            if (this.bInTrans)
            {
                command.Transaction = this.trans;
            }
            return command.ExecuteReader();
        }

        public override DataSet ExecQueryForDataSet(string strSql, string strTableName)
        {
            DataSet dataSet = new DataSet();
            OleDbDataAdapter adapter = new OleDbDataAdapter(strSql, this.conn);
            OleDbCommand command = this.conn.CreateCommand();
            command.CommandText = strSql;
            if (this.bInTrans)
            {
                command.Transaction = this.trans;
            }
            adapter.SelectCommand = command;
            adapter.Fill(dataSet, strTableName);
            return dataSet;
        }

        public override DataTable ExecQueryForDataTable(string strSql)
        {
            DataTable dataTable = new DataTable();
            OleDbDataAdapter adapter = new OleDbDataAdapter(strSql, this.conn);
            OleDbCommand command = this.conn.CreateCommand();
            command.CommandText = strSql;
            if (this.bInTrans)
            {
                command.Transaction = this.trans;
            }
            adapter.SelectCommand = command;
            adapter.Fill(dataTable);
            return dataTable;
        }

        public override object ExecScalar(string strSql)
        {
            OleDbCommand command = this.conn.CreateCommand();
            command.CommandText = strSql;
            if (this.bInTrans)
            {
                command.Transaction = this.trans;
            }
            return command.ExecuteScalar();
        }

        public override object ExecScalar(string strSql, IDataParameter[] parameters)
        {
            return this.GetPreCommand(strSql, parameters, "PROCEDURE").ExecuteScalar();
        }

        public override int ExecUpdateProc(string strSql, IDataParameter[] parameters)
        {
            return this.GetPreCommand(strSql, parameters, "PROCEDURE").ExecuteNonQuery();
        }

        public override int ExecUpdateSql(string strSql)
        {
            OleDbCommand command = this.conn.CreateCommand();
            command.CommandText = strSql;
            if (this.bInTrans)
            {
                command.Transaction = this.trans;
            }
            return command.ExecuteNonQuery();
        }

        private OleDbCommand GetPreCommand(string strSql, IDataParameter[] parameters, string strCommandType)
        {
            OleDbCommand command = this.conn.CreateCommand();
            command.CommandText = strSql;
            if (strCommandType == "PROCEDURE")
            {
                command.CommandType = CommandType.StoredProcedure;
            }
            if (this.bInTrans)
            {
                command.Transaction = this.trans;
            }
            foreach (IDataParameter parameter in parameters)
            {
                command.Parameters.Add(parameter);
            }
            return command;
        }

        public override void Open()
        {
            if (this.conn.State.Equals(ConnectionState.Closed))
            {
                this.conn.Open();
            }
        }

        public override void RollBackTran()
        {
            if (this.bInTrans)
            {
                this.trans.Rollback();
                this.bInTrans = false;
            }
        }

        public override IDbConnection baseConnection
        {
            get
            {
                return this.conn;
            }
            set
            {
                this.conn = (OleDbConnection) value;
            }
        }

        public override IDbTransaction baseTransaction
        {
            get
            {
                return this.trans;
            }
            set
            {
                this.trans = (OleDbTransaction) value;
            }
        }
    }
}

